#!/usr/bin/make

# defaults
COMPILER = gnu

# main building variables
ifeq "$(COMPILER)" "gnu"
  FC    = gfortran
  OPTSC = -cpp -c -frealloc-lhs -O2  -J $(DMOD)
  OPTSL = -J $(DMOD)
endif
ifeq "$(COMPILER)" "intel"
  FC    = ifort
  OPTSC = -c -O2 -module $(DMOD)
  OPTSL = -module $(DMOD)
endif
ifeq "$(COMPILER)" "ibm"
  FC    = bgxlf2008_r
  OPTSC = -c -O2 -qmoddir=$(DMOD) -I$(DMOD)
  OPTSL = -qmoddir=$(DMOD) -I$(DMOD)
endif
ifeq "$(COMPILER)" "cray"
  FC    = ftn
  OPTSC = -c -O 0 -e Z -g
endif
DSRC    = src/tests/accuracy/oscillation
DOBJ    = build/tests/accuracy/oscillation/obj/
DMOD    = build/tests/accuracy/oscillation/mod/
DEXE    = build/tests/accuracy/oscillation/
LIBS    = src/third_party/lib/flap/libflap.a build/lib/libfoodie.a src/third_party/lib/pyplot-fortran/libpyplot-fortran.a src/third_party/lib/wenoof/libwenoof.a
MODINC  = -Isrc/third_party/lib/flap/mod -Ibuild/lib/mod -Isrc/third_party/lib/pyplot-fortran/mod -Isrc/third_party/lib/wenoof/mod
VPATH   = $(DSRC) $(DOBJ) $(DMOD)
MKDIRS  = $(DBLD) $(DOBJ) $(DMOD)
LCEXES  = $(shell echo $(EXES) | tr '[:upper:]' '[:lower:]')
EXESPO  = $(addsuffix .o,$(LCEXES))
EXESOBJ = $(addprefix $(DOBJ),$(EXESPO))

#auxiliary variables
COTEXT = "Compiling $(<F)"
LITEXT = "Assembling $@"
MKLIBS = flap foodie pyplot wenoof

#building rules
$(DEXE)oscillation: $(MKDIRS) $(DOBJ)oscillation.o
	@rm -f $(filter-out $(DOBJ)oscillation.o,$(EXESOBJ))
	@echo $(LITEXT)
	@$(FC) $(OPTSL) $(DOBJ)*.o $(LIBS) -o $@
EXES := $(EXES) oscillation

#compiling rules
$(DOBJ)oscillation_t.o: src/tests/accuracy/oscillation/oscillation_t.f90 $(MKLIBS)
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $(MODINC) $< -o $@

$(DOBJ)oscillation.o: src/tests/accuracy/oscillation/oscillation.f90 \
	$(DOBJ)oscillation_t.o \
	$(MKLIBS)
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $(MODINC) $< -o $@

 # libraries
.PHONY : flap
flap:
	@rm -rf src/third_party/lib/flap
	@mkdir -p src/third_party/lib
	@$(MAKE) COMPILER="$(COMPILER)" STATIC=yes --directory=src/third_party/FLAP
	@mv src/third_party/FLAP/static src/third_party/lib/flap

.PHONY : foodie
foodie:
	@$(MAKE) -f makefile_foodie COMPILER="$(COMPILER)"

.PHONY : pyplot
pyplot:
	@rm -rf src/third_party/lib/pyplot-fortran/
	@mkdir -p src/third_party/lib/pyplot-fortran/mod
	@mkdir -p src/third_party/lib/pyplot-fortran/obj
	@$(FC) -c src/third_party/pyplot-fortran/src/pyplot_module.f90 -o pyplot_module.o
	@ar -rcs libpyplot-fortran.a pyplot_module.o ; ranlib libpyplot-fortran.a
	@mv libpyplot-fortran.a src/third_party/lib/pyplot-fortran/
	@mv pyplot_module.mod src/third_party/lib/pyplot-fortran/mod
	@mv pyplot_module.o src/third_party/lib/pyplot-fortran/obj

.PHONY : wenoof
wenoof:
	@rm -rf src/third_party/lib/wenoof
	@mkdir -p src/third_party/lib
	@$(MAKE) COMPILER="$(COMPILER)" STATIC=yes --directory=src/third_party/WenOOF
	@mv src/third_party/WenOOF/static src/third_party/lib/wenoof

#phony auxiliary rules
.PHONY : $(MKDIRS)
$(MKDIRS):
	@mkdir -p $@
.PHONY : cleanthirdpartylib
cleanthirdpartylib:
	@echo deleting third party libs
	@rm -fr src/third_party/lib
.PHONY : clean
clean:
	@echo deleting build
	@rm -fr build/
.PHONY : cleanall
cleanall: clean cleanthirdpartylib
